<?php

class Nuo_Html_Element_Form extends Nuo_Html_Element_Group
{
	protected $_name = 'val';

	protected $_meta = array(
		'tag' => 'form',
		'render' => 'div3'
	);

	protected $_attrib = array(
		'method' => 'post',
		'enctype' => 'multipart/form-data',
	);

	public function __construct()
	{
		$params = $_GET;
		if (isset($params['_p']))
		{
			unset($params['_p']);
		}

		$this -> setAction(Nuo::getUrl(Nuo::getRequest() -> getPath(), $params));
		
		$this -> init();
	}

	/**
	 * recheck is valid by call back process
	 */
	public function isValid($values)
	{
		if (parent::isValid($values))
		{
			return $this -> afterValidate();
		}

		return 0;
	}

	public function afterValidate()
	{
		return 1;
	}

	/**
	 * set form to ajax request.
	 * @return Nuo_Html_Element_Form
	 */
	function isAsync()
	{
		$this -> setAttrib('aria-submit', 'async');
		return $this;
	}

	/**
	 * @param string $url
	 * @return Nuo_Html_Element_Form
	 */
	function setAction($url)
	{
		$this -> _attrib['action'] = $url;
		return $this;
	}

	/**
	 * @param string $formKey
	 */
	static public function factory($formKey)
	{
		$cacheKey = 'form/' . $formKey;

		$data = Nuo_Cache::instance()-> read($cacheKey);

		if (FALSE == $data)
		{
			$data = Nuo::getTable('core/html/form') -> loadFormStructure($formKey);

			Nuo_Cache::instance()-> write($cacheKey, $data);

		}

		$form = new self;

		return $form -> setOptions($data);
	}

	static public function removeCache($formKey)
	{
		$cacheKey = 'form/' . $formKey;
		Nuo_Cache::instance()-> remove($cacheKey);

	}

}
